package com.jiaobuchong.kafka.producer.async;

import org.apache.kafka.clients.producer.*;

import java.util.Properties;

class MyProducerCallBack implements Callback {
    @Override
    public void onCompletion(RecordMetadata recordMetadata, Exception e) {
        if (e != null) {
            System.out.println("AsynchronousProducer failed with an exception");
        } else {
            System.out.println("Message is sent to  Partition no: " + recordMetadata.partition() +
                    " and offset " + recordMetadata.offset());
            System.out.println("AsynchronousProducer call success");
        }
    }
}

public class ASyncProduce {
    public static void main(String[] args) {
        String topicName = "SimpleProducerTopic";
        String key = "Key1";
        String value = "Value-1";

        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9093,localhost:9094");
        props.put("acks", "all");
        props.put("retries", 0);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        Producer<String, String> producer = new KafkaProducer<>(props);
        ProducerRecord<String, String> record = new ProducerRecord<>(topicName, key, value);
        producer.send(record, new MyProducerCallBack());
        producer.close();

        System.out.println("AsynchronousProducer call completed");

    }
}
